﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;

namespace RegionSplittor.Data
{
	public class BeamRegionData
	{
		public List<ElementId> GetBeamIdsByType(BeamOrderType type)
		{
			List<ElementId> list = new List<ElementId>();
			foreach (BeamCellData beamCellData in this.m_arBeamCell)
			{
				List<ElementId> beamIdsByType = beamCellData.GetBeamIdsByType(type);
				if (beamIdsByType.Count > 0)
				{
					list.AddRange(beamIdsByType);
				}
			}
			list = list.Distinct<ElementId>().ToList<ElementId>();
			return list;
		}

		public BeamCellData GetMaxBeamCell()
		{
			BeamCellData result = null;
			SortedDictionary<double, BeamCellData> sortCellData = this.GetSortCellData();
			if (sortCellData.Count > 0)
			{
				result = sortCellData.Last<KeyValuePair<double, BeamCellData>>().Value;
			}
			return result;
		}

		public BeamCellData GetMinBeamCell()
		{
			BeamCellData result = null;
			SortedDictionary<double, BeamCellData> sortCellData = this.GetSortCellData();
			if (sortCellData.Count > 0)
			{
				result = sortCellData.First<KeyValuePair<double, BeamCellData>>().Value;
			}
			return result;
		}

		private SortedDictionary<double, BeamCellData> GetSortCellData()
		{
			SortedDictionary<double, BeamCellData> sortedDictionary = new SortedDictionary<double, BeamCellData>();
			foreach (BeamCellData beamCellData in this.m_arBeamCell)
			{
				if (beamCellData.m_CellArea > 0.0 && !sortedDictionary.ContainsKey(beamCellData.m_CellArea))
				{
					sortedDictionary.Add(beamCellData.m_CellArea, beamCellData);
				}
			}
			return sortedDictionary;
		}

		public double GetBeamHeightByType(BeamOrderType type)
		{
			double result = 0.0;
			foreach (BeamCellData beamCellData in this.m_arBeamCell)
			{
				if (beamCellData.GetBeamIdsByType(type).Count > 0)
				{
					result = beamCellData.GetBeamHeightByType(type);
					break;
				}
			}
			return result;
		}

		public double GetBeamHeightById(ElementId id)
		{
			double result = 0.0;
			foreach (BeamCellData beamCellData in this.m_arBeamCell)
			{
				if (beamCellData.GetBeamIdsByType(BeamOrderType.BT_PrimaryBeam).Count > 0)
				{
					result = beamCellData.GetBeamHeightById(id);
					break;
				}
			}
			return result;
		}

		public Curve GetCurveByEleId(ElementId id)
		{
			Curve curve = null;
			foreach (BeamCellData beamCellData in this.m_arBeamCell)
			{
				curve = beamCellData.GetCellCurveById(id);
				if (null != curve)
				{
					break;
				}
			}
			return curve;
		}

		public List<CurveElementData> GetPrimaryCurveDatas()
		{
			return this.m_arPrimaryCurveData;
		}

		public List<BeamCellData> m_arBeamCell = new List<BeamCellData>();

		public int m_nCellCount;

		public RegionType m_regionType;

		public bool m_bHasSecondary;

		public List<CurveElementData> m_arPrimaryCurveData = new List<CurveElementData>();

		public Document m_regionDoc;
	}
}
